package sg.nus.iss.vmcs.models;

import sg.nus.iss.vmcs.VMCSException;

/**
 * Abstract Base class for Stores
 */
public abstract class Store {

	/** Size of the Store */
    protected int size;

    /** Store Items held by the store */
    protected StoreItem items[];

    /**
     * Creates an instance of a store
     */
    public Store(){}
    
    /**
     * Creates an instance of a store and initializes its size
     * @param itemn Number of items to contain
     */
    public Store (int itemn){
        setStoreSize(itemn);
    }

    /**
     * Sets the size of the store. Take note, this resets the store and its items
     * @param sz New size of the store.
     */
    public void setStoreSize(int sz) {
    	size = sz;
    	items = new StoreItem[size];
    }

    /**
     * Returns the Store Items held by the store
     * @return Store Item array
     */
    public StoreItem[] getItems() {
    	return items;
    }

    /**
     * Adds an item to the store
     * @param idx Index to place it at
     * @param object StoreItem to add
     * @throws VMCSException When index exceeds size of store
     */
    public void addItem(int idx, StoreItem object) throws VMCSException {
    	if(idx >= size) throw new VMCSException ("Store.getStoreItem", "Index overflow");
    	items[idx] = object;
    }

    /**
     * Returns a store item at a particular position
     * @param idx Index of the store item
     * @return Store Item
     * @throws VMCSException When index exceeds size of store
     */
    public StoreItem getStoreItem(int idx) throws VMCSException {
    	if(idx >= size) throw new VMCSException ("Store.getStoreItem", "Index overflow");
    	return items[idx];
    }
    
    /**
     * Gets the index of a store item
     * @param item Item to find
     * @return Index
     */
    public int getStoreItemIndex(StoreItem item) {
    	for(int i = 0; i < items.length; i++) {
    		if(items[i] == item) return i; 
    	}
    	
    	return -1;
    }

    /**
     * Finds an object within the StoreItems held by this store
     * @param name Name of the object
     * @return The actual object
     */
    public StoreObject findObject(String name) {
    	for(int i = 0; i < size; i++) {
    		if(items[i] != null) {
    			StoreObject content = items[i].getContent();
    			if(content != null && content.getName().compareTo(name) == 0) {
    					return content;
    			}
    		}
    	}
    	return null;
    }

    /**
     * Sets the quantity of a StoreItem within this store
     * @param item Index of Item to set
     * @param qty Quantity to set
     * @throws VMCSException When index exceeds size
     */
    public void setQuantity(int item, int qty) throws VMCSException {
    	if(item >= size) throw new VMCSException ("Store.setQuantity", "Index overflow");
    	items[item].setQuantity(qty);
    }

    /**
     * Returns the size of the store
     * @return Store Size
     */
    public int getStoreSize(){
    	return size;
    }
}
